package com.fs;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.MultipartConfigElement;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.fs.base.utils.ApplicationUtils;

@SpringBootApplication
@EnableCaching
public class Application  extends SpringBootServletInitializer{
	public static void main(String[] args) throws Exception {
		ApplicationContext applicationContext = SpringApplication.run(Application.class, args);
		new ApplicationUtils().setCtx(applicationContext);
	}
	
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
	
	/*@Bean
	public ActivityBehaviorFactory activityBehaviorFactory() {
		return new ActivityBehaviorFactoryExt();
	}
	
	@Bean
	public ExclusiveGatewayActivityBehaviorExt exclusiveGatewayActivityBehavior (){
		return new ExclusiveGatewayActivityBehaviorExt(); 
	}*/
	
	
	/**
	 * 更改附件上传大小限制
	 * @return
	 */
	@Bean   
    public MultipartConfigElement multipartConfigElement() {
	    MultipartConfigFactory factory = new MultipartConfigFactory();
	    //// 设置文件大小限制 ,超了，页面会抛出异常信息，这时候就需要进行异常信息的处理了;  
	    factory.setMaxFileSize("300MB"); //KB,MB
	    /// 设置总上传数据总大小
	    factory.setMaxRequestSize("300MB");
	    return factory.createMultipartConfig();
    }
	
	/**
	 * 采用fastJson作为json解析框架
	 * @return
	 */
	@Bean
    public HttpMessageConverters httpMessageConverters() {
        FastJsonHttpMessageConverter oFastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig oFastJsonConfig = new FastJsonConfig();
        oFastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        oFastConverter.setFastJsonConfig(oFastJsonConfig);
        //处理中文乱码问题
        List<MediaType> oFastMediaTypeList = new ArrayList<MediaType>();
        oFastMediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
        oFastConverter.setSupportedMediaTypes(oFastMediaTypeList);

        HttpMessageConverter<?> oConverter = oFastConverter;
        return new HttpMessageConverters(oConverter);
    }
}